﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Configuration;

namespace Cheka.WinForm.Sample.Course
{
    public partial class MainForm : Form, ICourseView
    {
        // ==================================================== //
        #region "member variables"

        private SemesterGrpPresenter m_presenter;

        #endregion

        // ==================================================== //
        #region "constructors"

        public MainForm()
        {
            InitializeComponent();

            dgvCourses.AutoGenerateColumns = false;

            m_presenter = new SemesterGrpPresenter(ConfigurationManager.AppSettings["DataSource"], this);
        }

        #endregion

        // ==================================================== //
        #region "override methods"

        protected override void OnLoad(EventArgs e)
        {
            m_presenter.OnLoad();
            menuEdit.DropDown = contextMenu;
            base.OnLoad(e);
        }

        #endregion

        // ==================================================== //
        #region "implement ICourseView"

        public string CurrentGrpName
        {
            get { return cmbSemester.Text; }
            set { cmbSemester.Text = value; }
        }

        public void DisplayCourses(IList<Course> courses)
        {
            dgvCourses.DataSource = null;
            dgvCourses.DataSource = courses;
        }

        public void FillGroupNames(IList<string> names)
        {
            cmbSemester.DataSource = names;
        }

        public void ShowMessage(string message)
        {
            statusLabelTips.Text = message;
        }

        #endregion

        // ==================================================== //
        private void cmbSemester_SelectionChangeCommitted(object sender, EventArgs e)
        {
            m_presenter.OnCurSemesterChanged();
        }

        private void menuItemSave_Click(object sender, EventArgs e)
        {
            m_presenter.OnSave();
        }

        private void toolBtnSave_Click(object sender, EventArgs e)
        {
            menuItemSave.PerformClick();
        }

        private void menuItemAddCourse_Click(object sender, EventArgs e)
        {
            using (CourseDlg dialog = new CourseDlg())
            {
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    m_presenter.OnAddCourse(dialog.Course);
                }
            }
        }

        private void menuItemRemoveCourse_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = dgvCourses.SelectedRows[0];
            m_presenter.OnRemoveCourse(row.DataBoundItem as Course);
            
            // !!! runtime exception due to bound data source not implement "IBindingList"
            // dgvCourses.Rows.RemoveAt(row.Index);
        }
    }
}
